package datastructure.sort;

/**
 * @author MaoLin Wang
 * @description
 * @date 2020-08-24 15:22
 */
public class SelectSort {
    public static void selectSort(int[]arr){
        for (int i = 0; i < arr.length; i++) {
            int min=arr[i];
            int minIndex=i;

            for (int j = i+1; j < arr.length; j++) {
                if (arr[j]<min){
                    min=arr[j];
                    minIndex=j;
                }
            }
            if (minIndex!=i){
                arr[minIndex]=arr[i];
                arr[i]=min;
            }
        }
    }

    public static void selectSort2(int[]arr) {
        if (arr.length<=1){
            return;
        }

        for (int i = 1; i <arr.length-1; i++) {
            int min=arr[i];
            int minIndex=i;

            for (int j = i+1; j <arr.length ; j++) {
                if (arr[j]<min){
                    minIndex=j;
                    min=arr[j];
                }
            }
            if (minIndex!=i){
                arr[minIndex]=arr[i];
                arr[i]=min;
            }
        }
    }

        public static void main(String[] args) {
        int[]arr=new int[]{2,4,51,33,4,74,34,21};
        selectSort2(arr);
        for (int i : arr) {
            System.out.println(i);
        }
    }
}
